/*
	StructureShipyard.cpp
	(c)2000 Palestar Inc, Richard Lyle
*/


#include "Resource.h"
#include "GameContext.h"
#include "StructureShipyard.h"
#include "TraitOrbit.h"

//----------------------------------------------------------------------------

Constant USE_SHIPYARD_RANGE( "USE_SHIPYARD_RANGE", 250.0f );

//----------------------------------------------------------------------------

IMPLEMENT_FACTORY( StructureShipyard, NounStructure );
REGISTER_FACTORY_KEY( StructureShipyard, 4435834781079288815 );

StructureShipyard::StructureShipyard()
{}

//----------------------------------------------------------------------------

int StructureShipyard::maxDamage() const
{
	return 35000;
}

int StructureShipyard::buildTechnology() const
{
	return 65;
}

int	StructureShipyard::buildTime() const
{
	return 270;		
}

int	StructureShipyard::buildCost() const
{
	return 20000;
}

int	StructureShipyard::repairRate() const
{
	return 500;
}

Color StructureShipyard::color() const
{
	return BLUE;
}

dword StructureShipyard::planetFlags() const
{
	return NounPlanet::FLAG_HAS_SHIPYARD;
}

int	StructureShipyard::workers() const
{
	return 40;
}

int	StructureShipyard::power() const
{
	return -75;
}

int StructureShipyard::technology() const
{
	return 65;
}

bool StructureShipyard::usable( NounShip * pShip ) const
{
	if ( !active() )
		return false;		// shipyard is not active
	if ( pShip == NULL || pShip->parent() == NULL )
		return false;		// invalid ship or ship already detached
	NounPlanet * pPlanet = planet();
	if ( pPlanet == NULL )
		return false;		// structure not on planet
	if ( (pPlanet->flags() & NounPlanet::FLAG_BLOCKADE) != 0 )
		return false;		// can't, planet is under blockade..
	if ( pShip->orbiting() != pPlanet && (pShip->worldPosition() - worldPosition()).magnitude() > USE_SHIPYARD_RANGE )
		return false;		// ship not in orbit or out of range
	if (! pShip->isFriend( pPlanet ) )
		return false;		// enemy ship
	
	return true;
}

CharString StructureShipyard::useTip( NounShip * pShip ) const
{
	return "USE SHIPYARD";
}

bool StructureShipyard::useConfirm( NounShip * pShip ) const
{
	return true;	
}

CharString StructureShipyard::useConfirmMessage( NounShip * pShip ) const
{
	return "<color;ffffff>Dock Master: Confirm Ship Change";
}

void StructureShipyard::use( dword nWhen, NounShip * pShip )
{
	// just detach the players ship from the universe
	if ( usable( pShip ) )
	{
		// move ship out of orbit, so it doesn't snap back to this planet when they respawn
		TraitOrbit * pOrbit = FindTrait<TraitOrbit>( pShip );
		if ( pOrbit != NULL )
			pOrbit->breakOrbit();

		// set the ship home to this planet, so they must respawn at this position
		pShip->setHome( planet() );
		// detach the ship from the universe
		if ( context()->server() )
			pShip->setDetach();
	}
}

//----------------------------------------------------------------------------
//EOF
